/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "cpputils/CppUtils.hpp"
#include "cpputils/io/StreamReframer.hpp"

namespace cpputils {
	namespace io {
		// --------------------------------------------------------------------------------------------------
		StreamReframer::StreamReframer() {
			this->m_nFrameSize = 0;
			this->m_poDestination = NULL;
		}

		// --------------------------------------------------------------------------------------------------
		StreamReframer::~StreamReframer() {
		}

		// --------------------------------------------------------------------------------------------------
		void StreamReframer::setDestination(IOutputStream* poDestination) {
			this->m_poDestination = poDestination;
		}

		// --------------------------------------------------------------------------------------------------
		IOutputStream* StreamReframer::getDestination() const {
			return this->m_poDestination;
		}

		// --------------------------------------------------------------------------------------------------
		void StreamReframer::setFrameSize(unsigned int nFrameSize) {
			this->m_nFrameSize = nFrameSize;
		}

		// --------------------------------------------------------------------------------------------------
		const unsigned int StreamReframer::getFrameSize() const {
			return this->m_nFrameSize;
		}

		// --------------------------------------------------------------------------------------------------
		void StreamReframer::write(ByteBuffer* poBuffer) {
			if( this->m_poDestination==NULL )
				throw SystemException("Unable to write to NULL destination");
			if(this->m_nFrameSize == 0)
				throw SystemException("Unable to write to destination with the zero frame size");

			// If the input buffer is smaller than specified frame size
			if(poBuffer->getLen() < this->m_nFrameSize) {
				this->m_poDestination->write(poBuffer);
			}
			else {
				unsigned int nCurrentReadPos = 0; // A current read position in the input buffer
				ByteBuffer oTempBuffer;
				do {
					unsigned int nCurrentFrameSize = this->m_nFrameSize;
					if(nCurrentFrameSize > poBuffer->getLen()-nCurrentReadPos)
						nCurrentFrameSize = poBuffer->getLen()-nCurrentReadPos;

					//Setting up new temp buffer without coping data and with disabled auto-dispose feature
					oTempBuffer.setup(poBuffer->getData() + nCurrentReadPos, nCurrentFrameSize, false, false);
					this->m_poDestination->write(&oTempBuffer);

					nCurrentReadPos+=nCurrentFrameSize;
				}
				while(nCurrentReadPos<poBuffer->getLen());
			}
		}
	}
}
